package org.haredot.controller;

import org.haredot.annotation.LogModule;
import org.haredot.entity.Role;
import org.haredot.service.RoleService;
import org.haredot.vo.ResultVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@LogModule("角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService ;

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('role:view:add')")
    public ResultVo<?> addRole(@RequestBody Role role) {

        return roleService.saveRole(role);
    }

    @PutMapping("/modify{roleId}")
    @PreAuthorize("hasAuthority('role:view:modify')")
    public ResultVo<?> modifyRole(@PathVariable("roleId") Long roleId, @RequestBody Role role) {

        role.setId(roleId);

        return roleService.modifyRole(role);
    }

    @DeleteMapping("/remove/{roleId}")
    @PreAuthorize("hasAuthority('role:view:remove')")
    public ResultVo<?> removeRole(@PathVariable("roleId") Long roleId) {
        return roleService.removeRole(roleId) ;
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('role:view')")
    public ResultVo<?> list() {
        return roleService.queryRoles();
    }

    @GetMapping("/tree/{roleId}")
    @PreAuthorize("hasAuthority('role:view:permission')")
    public ResultVo<?> tree(@PathVariable("roleId") Long roleId) {
        return roleService.queryTreeNodes(roleId);
    }

    @PostMapping("/bindPermission/{roleId}")
    @PreAuthorize("hasAuthority('role:view:permission')")
    public ResultVo<?> bindPermission(@PathVariable Long roleId, @RequestBody List<String> nodes) {

        return roleService.bindPermission(roleId, nodes);
    }
}
